-- 斡衡技能定义（X改为当前体力值）
local bozhiheng = fk.CreateSkill {
  name = "bozhiheng",
}

Fk:loadTranslationTable{
  ["bozhiheng"] = "斡衡",
  [":bozhiheng"] = "出牌阶段或当你受到伤害后，你可以令一名其他角色摸或弃置X张牌（X为你的当前体力值）。然后若其手牌数与你不同或X大于或等于5，"..
  "你摸4张牌且此技能本回合失效。",
  ["#bozhiheng"] = "斡衡：你可以令一名角色摸或弃置%arg张牌",
  ["$bozhiheng1"] = "壁立以千仞，非蚍蜉可撼。",
  ["$bozhiheng2"] = "朕德载后土，焉不容天下风雨。",
  ["$bozhiheng3"] = "制衡联合，稳而不乱。", 
}

bozhiheng:addEffect("active", {
  anim_type = "control",
  prompt = function (self, player, selected_cards, selected_targets)
    return "#bozhiheng:::" .. player.hp  -- 使用当前体力值
  end,
  card_num = 0,
  target_num = 1,
  interaction = UI.ComboBox {choices = { "draw_card", "discard_skill" } },
  can_use = Util.TrueFunc,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local currentHp = player.hp  -- 获取当前体力值
    
    -- 执行摸牌或弃牌（X为当前体力值）
    if self.interaction.data == "draw_card" then
      target:drawCards(currentHp, bozhiheng.name)
    else
      room:askToDiscard(target, {
        min_num = currentHp,
        max_num = currentHp,
        include_equip = true,
        skill_name = bozhiheng.name,
        cancelable = false,
      })
    end
    
    if player.dead then return end
    
    -- 触发条件：目标手牌数≠自己 或 当前体力值≥5
    if target:getHandcardNum() ~= player:getHandcardNum() or currentHp >= 5 then
      room:invalidateSkill(player, bozhiheng.name, "-turn")  -- 技能本回合失效
      player:drawCards(4, bozhiheng.name)  -- 摸4张牌
    end
  end,
})

bozhiheng:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(bozhiheng.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local currentHp = player.hp  -- 获取当前体力值
    
    -- 询问是否发动技能，提示显示当前体力值
    local success, dat = room:askToUseActiveSkill(player, {
      skill_name = bozhiheng.name,
      prompt = "#bozhiheng:::" .. currentHp,
      cancelable = true,
      skip = true,
    })
    
    if success and dat then
      event:setCostData(self, {tos = dat.targets, choice = dat.interaction})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local tos = event:getCostData(self).tos
    local skill = Fk.skills[bozhiheng.name]
    skill.interaction = skill.interaction or {}
    skill.interaction.data = event:getCostData(self).choice
    
    -- 调用主动效果逻辑
    skill:onUse(player.room, {
      from = player,
      tos = tos,
    })
  end,
})

return bozhiheng